package com.jw.debezuim.websocket.config;

import com.jw.debezuim.websocket.SimulateWebSocketHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * @author jw
 * @Title: WebSocketConfig
 * @Description: websocket配置文件
 * @date 2018/7/2717:55
 */
@Configuration
@EnableWebSocket
@ConditionalOnProperty(value = {"biz.server.web-socket.enable"})
public class SpringWebSocketConfig implements WebSocketConfigurer {

    @Autowired
    private SimulateWebSocketHandler simulateWebSocketHandler;

    @Autowired
    private WebSocketInterceptor webSocketInterceptor;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        //handler是webSocket的核心，配置入口
        registry.addHandler(simulateWebSocketHandler, "/ws/**").setAllowedOrigins("*").addInterceptors(webSocketInterceptor);
    }
}
